
***************************************************************************************************************
/*
THIS DO-FILE:
Gets 2000 (wave 5) helper information from raw HRS data
*/
***************************************************************************************************************

clear
set more off
set maxvar 10000

***************************************************************************************************************
use "$rawdata\h00core\h00sta\H00E_R.DTA"
merge 1:m HHID PN using "$rawdata\h00core\h00sta\H00E_HP.DTA", keep(3)

egen LOPN=concat(GSUBHH OPN)

destring HHID, replace
destring PN, replace
gen double hhidpn=1000*HHID+PN

destring OPN, replace 
destring LOPN, replace 
gen double kidid=10000*HHID+LOPN

*adls: opn of up to 7 helpers
foreach var in G2800 G2814 G2823 G2827 G2831 G2835 G2839 {
destring `var', replace
}

*iadls: opn of up to 8 helpers (includes money)
foreach var in G2880 G2889 G2894 G2900 G2906 G2912 G2919 G2923 {
destring `var', replace
}

*helper type
gen helper_spouse=(G2947A==10) if G2947A!=.
gen helper_child=inlist(G2947A,11,12,13,14,15) if G2947A!=.
gen helper_othfam=inlist(G2947A,21,22,31,32,41,51,52) if G2947A!=.
gen helper_nonfam=inlist(G2947A,61,62,63,71,72,73) if G2947A!=.
foreach var in helper_spouse helper_nonfam helper_child helper_othfam {
	replace `var'=. if helper_spouse==0 & helper_nonfam==0 & helper_child==0 & helper_othfam==0
}
*2 out of 4156 are missing -- they won't be counted as helpers

*helper paid?
gen helper_paid=(G2957==1) if helper_nonfam==1
foreach var in G2800 G2814 G2823 G2827 G2831 G2835 G2839 {
	replace helper_paid=1 if `var'==100 & OPN==`var' //helper is employee of facility
}
foreach var in G2880 G2889 G2894 G2900 G2906 G2912 G2919 G2923 {
	replace helper_paid=1 if `var'==100 & OPN==`var' //helper is employee of facility
}

*helper frequency - converted to days per month
gen 	helper_dpm=G2950 if G2950<=31  //reported days per month
replace helper_dpm=G2951*4 if G2951<=7 //reported days per week
replace helper_dpm=30 if G2952==1	   //reported every day
replace helper_dpm=G2976 if G2976<=31 & helper_dpm==.
replace helper_dpm=G2977*4 if G2977<=7 & helper_dpm==.
replace helper_dpm=30 if G2978==1 & helper_dpm==.
replace helper_dpm=0 if helper_dpm==. & (G2950==96 | G2976==96)
*impute the "DK" responses with median for that type of helper
forval x=10/73 {
	qui sum helper_dpm if G2947A==`x', det
	qui replace helper_dpm=r(p50) if G2947A==`x' & (inlist(G2950,98,99) | G2951==8 | G2952==8 | inlist(G2976,98,99)) & helper_dpm==.
}

*helper hours per day
gen 	helper_hpd=G2954 if G2954<=24
replace helper_hpd=G2980 if G2980<=24 & helper_hpd==.
*impute the "DK" responses by family/other and days per month categories
foreach var in helper_spouse helper_child helper_othfam helper_nonfam {
	sum helper_hpd if `var'==1 & helper_dpm>=1 & helper_dpm<=7, det
	replace helper_hpd=r(p50) if `var'==1 & helper_dpm>=1 & helper_dpm<=7 & (inlist(G2954,98,99) | inlist(G2980,98,99)) & helper_hpd==.
	sum helper_hpd if `var'==1 & helper_dpm>=8 & helper_dpm<=27, det
	replace helper_hpd=r(p50) if `var'==1 & helper_dpm>=8 & helper_dpm<=27 & (inlist(G2954,98,99) | inlist(G2980,98,99)) & helper_hpd==.
	sum helper_hpd if `var'==1 & helper_dpm>=28 & helper_dpm<=31, det
	replace helper_hpd=r(p50) if `var'==1 & helper_dpm>=28 & helper_dpm<=31 & (inlist(G2954,98,99) | inlist(G2980,98,99)) & helper_hpd==.
}

*hours per month
gen		helper_hpm=helper_hpd*helper_dpm
*those who report 0 or . dpm or hpd won't be counted as helpers unless AL/NH helper

*type of care
egen helper_fam=rowtotal(helper_spouse helper_child helper_othfam), missing
bys hhidpn: egen rec_inf=max(helper_fam)
bys hhidpn: egen rec_form=max(helper_nonfam)
bys hhidpn: egen rec_paid=max(helper_paid)

*hours - total over all of person's helpers
gen hrs_inf_temp=helper_hpm if helper_fam==1
bys hhidpn: egen hrs_inf=sum(hrs_inf_temp)
gen hrs_form_temp=helper_hpm if helper_nonfam==1
bys hhidpn: egen hrs_form=sum(hrs_form_temp)
egen hrs_total=rowtotal(hrs_inf hrs_form)

***************************************************************************************************************
*ADL AND IADL (including money) HELPERS (up to 7)
preserve
keep if G2800==OPN | G2814==OPN | G2823==OPN | G2827==OPN | G2831==OPN | G2835==OPN | G2839==OPN | ///
		G2880==OPN | G2889==OPN | G2894==OPN | G2900==OPN | G2906==OPN | G2912==OPN | G2919==OPN | G2923==OPN

keep hhidpn OPN LOPN kidid helper_hpm helper_spouse helper_child helper_othfam helper_nonfam helper_paid ///
	 rec_inf rec_form hrs_inf hrs_form hrs_total
foreach var in helper_hpm helper_spouse helper_child helper_othfam helper_nonfam helper_paid rec_inf rec_form hrs_inf hrs_form hrs_total {
	rename `var' `var'5
}
duplicates drop
save "allADLIADLhelpers_5.dta", replace
restore
***************************************************************************************************************

